<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'待发送红包列表'),
	array('op'=>'ok','title'=>'已发送红包列表'),	
	array('op'=>'no','title'=>'发送失败红包列表'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=0;
	if(!empty($_RQ['otitle'])){
		$oid=pdo_getcolumn('jy_onlineedu_video',array('uniacid'=>$_SESSION['uniacid'],'title like %'=>$_RQ['otitle'].'%'),'id');
		$params['oid']=intval($_RQ['oid']);
		$return['params']['oid']=$_RQ['oid'];
	}
	if(!empty($_RQ['nickname'])){
		$mid=pdo_getcolumn('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'nickname like %'=>$_RQ['nickname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','ordersn','oid','paytype','mid','fee','user_ip','ispay','status','createtime');
	$list=pdo_getslice('jy_onlineedu_redorder',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$opus=pdo_get('jy_onlineedu_video',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['oid']));
			$user=pdo_get('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$juser=pdo_get('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$opus['mid']));
			$item['otitle']=$opus['title'];
			$item['fnickname']=$user['nickname'];
			$item['favatar']=$user['avatar'];
			$item['jnickname']=$juser['nickname'];
			$item['javatar']=$juser['avatar'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单编号','type'=>'string'),
			array('name'=>'otitle','title'=>'群名称','type'=>'string'),
			array('name'=>'fnickname','title'=>'发送人昵称','type'=>'string'),
			array('name'=>'favatar','title'=>'发送人头像','type'=>'image'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
			array('name'=>'jnickname','title'=>'接收人昵称','type'=>'string'),
			array('name'=>'javatar','title'=>'接收人头像','type'=>'image'),
			array('name'=>'ispay','title'=>'支付?','type'=>'radio','options'=>'ispay'),
			array('name'=>'status','title'=>'红包代发','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action' =>array (
			    array('type'=>'post','op'=>'pass','show'=>array('ispay'=>1),'title'=>'审核通过','message'=>'确定通过该申请吗，通过后提现金额将发送到用户钱包！'),
			    array('type'=>'post','op'=>'unpass','show'=>array('ispay'=>1),'title'=>'审核不通过','message'=>'确定拒绝该申请吗！'),
			),
		),
		'search'=>array(
			array('name'=>'otitle','width'=>15,'title'=>'群名称','type'=>'string'),
			array('name'=>'fnickname','width'=>15,'title'=>'发送人昵称','type'=>'string'),
		)
	);
	$return['ispay']=array('0'=>'待支付','1'=>'已支付','2'=>'支付失败');
	$return['status']=array('0'=>'待审核','1'=>'已发送','2'=>'发送失败');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='ok'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=1;
	if(!empty($_RQ['otitle'])){
		$oid=pdo_getcolumn('jy_onlineedu_video',array('uniacid'=>$_SESSION['uniacid'],'title like %'=>$_RQ['otitle'].'%'),'id');
		$params['oid']=intval($_RQ['oid']);
		$return['params']['oid']=$_RQ['oid'];
	}
	if(!empty($_RQ['nickname'])){
		$mid=pdo_getcolumn('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'nickname like %'=>$_RQ['nickname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','ordersn','oid','paytype','mid','fee','user_ip','ispay','status','createtime');
	$list=pdo_getslice('jy_onlineedu_redorder',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$opus=pdo_get('jy_onlineedu_video',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['oid']));
			$reply=pdo_get('jy_onlineedu_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$opus['rid']));
			$user=pdo_get('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$juser=pdo_get('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$opus['mid']));
			$item['otitle']=$opus['title'];
			$item['rtitle']=$reply['title'];
			$item['fnickname']=$user['nickname'];
			$item['favatar']=$user['avatar'];
			$item['jnickname']=$juser['nickname'];
			$item['javatar']=$juser['avatar'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单编号','type'=>'string'),
			array('name'=>'otitle','title'=>'群名称','type'=>'string'),
			array('name'=>'fnickname','title'=>'发送人昵称','type'=>'string'),
			array('name'=>'favatar','title'=>'发送人头像','type'=>'image'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
			array('name'=>'jnickname','title'=>'接收人昵称','type'=>'string'),
			array('name'=>'javatar','title'=>'接收人头像','type'=>'image'),
			array('name'=>'ispay','title'=>'支付?','type'=>'radio','options'=>'ispay'),
			array('name'=>'status','title'=>'红包代发','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'otitle','width'=>15,'title'=>'群名称','type'=>'string'),
			array('name'=>'fnickname','width'=>15,'title'=>'发送人昵称','type'=>'string'),
		)
	);
	$return['status']=array('0'=>'待审核','1'=>'已发送','2'=>'发送失败');
	$return['ispay']=array('0'=>'待支付','1'=>'已支付','2'=>'支付失败');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='no'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=2;
	if(!empty($_RQ['otitle'])){
		$oid=pdo_getcolumn('jy_onlineedu_video',array('uniacid'=>$_SESSION['uniacid'],'title like %'=>$_RQ['otitle'].'%'),'id');
		$params['oid']=intval($_RQ['oid']);
		$return['params']['oid']=$_RQ['oid'];
	}
	if(!empty($_RQ['nickname'])){
		$mid=pdo_getcolumn('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'nickname like %'=>$_RQ['nickname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','ordersn','oid','paytype','mid','fee','user_ip','ispay','status','createtime','error_msg');
	$list=pdo_getslice('jy_onlineedu_redorder',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$opus=pdo_get('jy_onlineedu_video',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['oid']));
			$reply=pdo_get('jy_onlineedu_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$opus['rid']));
			$user=pdo_get('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$juser=pdo_get('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$opus['mid']));
			$item['otitle']=$opus['title'];
			$item['rtitle']=$reply['title'];
			$item['fnickname']=$user['nickname'];
			$item['favatar']=$user['avatar'];
			$item['jnickname']=$juser['nickname'];
			$item['javatar']=$juser['avatar'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单编号','type'=>'string'),
			array('name'=>'otitle','title'=>'群名称','type'=>'string'),
			array('name'=>'fnickname','title'=>'发送人昵称','type'=>'string'),
			array('name'=>'favatar','title'=>'发送人头像','type'=>'image'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
			array('name'=>'jnickname','title'=>'接收人昵称','type'=>'string'),
			array('name'=>'javatar','title'=>'接收人头像','type'=>'image'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'error_msg','title'=>'错误原因','type'=>'string'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action' =>array (
			    array('type'=>'post','op'=>'pass','title'=>'审核通过','message'=>'确定通过该申请吗，通过后提现金额将发送到用户钱包！'),
			    array('type'=>'post','op'=>'unpass','title'=>'审核不通过','message'=>'确定拒绝该申请吗！'),
			),
		),
		'search'=>array(
			array('name'=>'otitle','width'=>15,'title'=>'群名称','type'=>'string'),
			array('name'=>'fnickname','width'=>15,'title'=>'发送人昵称','type'=>'string'),
		)
	);
	$return['status']=array('0'=>'待审核','1'=>'已发送','2'=>'发送失败','3'=>'已拒绝');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_onlineedu_redorder',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}

if($_RQ['op']=='unpass'){
	   $id=intval($_RQ['id']);
	   if(!$id){
		   exi('该申请不存在，请重新选择！','error');
	   }
	   $redlog=pdo_get('jy_onlineedu_redorder',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	   $user=pdo_get('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$redlog['mid']));
		pdo_update('jy_onlineedu_redorder',array('status'=>3),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	   exi('拒绝申请成功','success');
}
  
if($_RQ['op']=='pass'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('该申请不存在，请重新选择！','error');
	}
	$redlog=pdo_get('jy_onlineedu_redorder',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$user=pdo_get('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$redlog['mid']));
	$fans=pdo_get('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$redlog['mid']));
	$basic=$this->plugin_setting('basic');
	$arr['openid'] = $fans['openid'];
	$arr['hbname'] = '群互推';
	$arr['body'] = "群互推红包";
	$arr['fee'] = $redlog['money'];
	$arr['mchid'] = $basic['mchid'];
	$arr['mch_appid'] = $basic['appid'];
	$arr['username'] = $user['nickname']?$user['nickname']:$fans['nickname'];
	$res = sendhongbaoto($arr);
	if($res['result_code'] == 'SUCCESS'){
		pdo_update('jy_onlineedu_user',array('money_get +='=>$redlog['fee']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$user['mid']));
		pdo_update('jy_onlineedu_redorder',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('发送成功，金额已转入到对方钱包！');
		
	}else{
		$res['result_msg']=$res['result_msg']?$res['result_msg']:'检查微信商户是否已设置,商户金额等';
		exi('提现失败：'.$res['result_msg'],'error');
	}
	
}